<template>
    <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
        <span class="el-form_span">手机号登录</span>
        <el-form-item label="手机号" prop="phoneNumber" class="Verification-_input ">
            <el-input v-model="ruleForm.phoneNumber" type="text" autocomplete="off" />
            <span class="Verification-_input_span" @click="smsSend">{{ smsStr }}</span>
        </el-form-item>
        <el-form-item label="验证码" prop="Verification">
            <el-input v-model="ruleForm.Verification" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="LoginForm(ruleFormRef)" style="width: 200px;">登录</el-button>
        </el-form-item>
        <span class="form_span" @click="switchSmsLogin">
            {{ isopen ? "" : "手机用户名登录" }}
        </span>
    </el-form>
</template>
<script setup>
import { reactive, ref, onBeforeUnmount } from "vue"
import emitter from "../../utils/bus";

import { checkAge, validatephone, useSubmitForm } from "../../utils/validata/validata.js"
import *as api from "../../api/user.js"
import { useUser } from "../../store/uses.js"
import { useRouter } from "vue-router"


let rules = reactive({
    Verification: [{ validator: checkAge, trigger: "blur" }],
    phoneNumber: [{ validator: validatephone, trigger: "blur" }],
});


const ruleForm = reactive({
    Verification: '',
    phoneNumber: ''
})

const router = useRouter()//路由的实例

const ruleFormRef = ref()

let getuser = useUser()//store的实例

let smsStr = ref("点击发送验证码")//span的文字

let successForm = async () => {
    try {
        //校验手机验证码
        let res = await api.SmsPhoneNumberLogin(ruleForm.Verification)
        router.push({
            name: 'home'
        })
        getuser.$patch((state) => {
            state.usersifno = reslogin.data.userInfo
        })
    } catch (error) {

        console.log(error);
    }
    console.log("successForm");
}

let failForm = () => {

    console.log("failForm");
}

let LoginForm = useSubmitForm(successForm, failForm)

let timer = ref(null)




let isopen = ref(false)

let switchSmsLogin = () => {

    emitter.emit("isopen", isopen.value = !isopen.value)

}


let falg = false
//发送验证码方法
let smsSend = async () => {

    try {
        let res = await api.SmsPhoneNumberCode(ruleForm.phoneNumber)

        console.log(res);
        if (!falg) {
            console.log("ww");
            doneTime(20, "点击发送验证码", () => {
                falg = false
            })
        }
        falg = true
    } catch (error) {
        falg = false
        console.log(error);
        return
    }
    falg = true
}


//避免内存泄露，切换页面是要停止定时器
let doneTime = (time = 10, endMgs, cd) => {
    let count = time
    timer = setInterval(() => {
        console.log(8888)
        smsStr.value = `${count}s后发送`
        console.log(count, "uueue");
        count--
        if (count < 0) {
            clearInterval(timer)
            smsStr.value = endMgs
            if (cd) {
                cd()
            }
            return
        }
    }, 1000)
}


// let checkcode = async () => {
//     let res = await api.SmsPhoneNumberLogin(ruleForm.password)
// }

onBeforeUnmount(() => {
    clearInterval(timer.value)
})

</script>

<style lang="less" scoped>
.el-form-item__content {
    .el-input {
        width: 200px;
    }
}

.el-form {
    background-color: rgba(0, 0, 0, .3);
    width: 400px;
    height: 400px;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    position: absolute;
    bottom: 10%;
    right: 7%;

    .el-form-item__label {
        color: rgb(223, 210, 210);
    }

}

.from-el-conter {
    position: relative;
    height: 100vh;

}

.videoItem {
    position: fixed;
    width: 100vw;
    z-index: -39857934;
    // height: 100vh;
}

.Verification-_input {
    :deep(.el-input__wrapper) {
        border-top-right-radius: 0px;
        border-bottom-right-radius: 0px;

    }

    :deep(.el-input__wrapper) {
        box-shadow: none;
    }

    .el-input {
        width: 110px;

    }
}

.el-form-item__content {
    display: flex;
    flex: 1;


    .Verification-_input_span {
        background-color: rgb(248, 245, 245);
        width: 90px;
        border-top-right-radius: 3px;
        border-bottom-right-radius: 3px;
        border-right: none;
        font-size: 12px;
        cursor: pointer;
    }
}

.el-form_span {
    text-align: center;
    margin: 0 0 60px 0;
    font-size: 32px;
    color: #fff;
}

.el-form-item {
    :deep(.el-form-item__label) {
        color: rgb(235, 232, 232);
    }
}

.form_span {
    color: rgb(234, 218, 218);
    text-align: center;
    cursor: pointer;

    &:hover {
        color: red;
    }
}
</style>